package com.inei.evaluaciondelcurso;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Xml;

public class Exportar {

	Context context;

	public Exportar(Context context) {
		this.context = context;
	}

	public boolean xml(List<String> puntajes) {
		File file = null;
		File ruta = null;
		String imei = "";

		try {
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			imei = tm.getDeviceId();
		} catch (Exception e) {
			imei = "";
		}

		try {
			File root = Environment.getExternalStorageDirectory();
			ruta = new File(root, "/Evaluacion del Curso/");
			if (!ruta.exists()) {
				ruta.mkdirs();
			}
			file = new File(ruta, "E_Curso.xml");
			FileOutputStream fos = new FileOutputStream(file);
			XmlSerializer xml = Xml.newSerializer();
			xml.setOutput(fos, HTTP.UTF_8);
			xml.startDocument(HTTP.UTF_8, true);
			xml.startTag("", "respuesta");

			
			if (imei != null) {
				xml.startTag("", "Tablet_id");
				xml.text(imei);
				xml.endTag("", "Tablet_id");
			}
			xml.startTag("", "formato_id");
			xml.text("" + 1);
			xml.endTag("", "formato_id");
			
			xml.startTag("", "etapa");
			xml.text("" + 2);
			xml.endTag("", "etapa");
			
			for (int i = 0; i < 10; i++) {
				String p = completar("" + (i + 1));
				xml.startTag("", p);
				xml.text("" + puntajes.get(i));
				xml.endTag("", p);
			}
			xml.endTag("", "respuesta");
			xml.endDocument();
			fos.close();

		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public String completar(String texto) {
		return "P" + (texto.length() == 1 ? "0" + texto : texto);
	}
}
